package com.weiyuwqj.admin.controller.admin.financial;

import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.convert.AccountBankAddtimeConvert;
import com.weiyuwqj.api.model.convert.AccountCashStatusConvert;
import com.weiyuwqj.api.model.convert.ItemAddtimeConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.convert.SysFeeTypeConvert;
import com.weiyuwqj.api.model.vo.SysFeeVo;
import com.weiyuwqj.api.service.SysFeeService;
import com.weiyuwqj.api.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2017/4/25.
 */
@Controller
@RequestMapping("/sys/fee")
public class SysFeeController {

    @Autowired
    private SysFeeService sysFeeService;

    //手续费管理列表
    @ResponseBody
    @RequestMapping(value ="/list",method= RequestMethod.POST)
    public Object list(Paginator p,
                       Integer status,
                       String source,
                       String orderNo,
                       String beginDateTime,
                       String endDateTime){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",status);
        map.put("source",source);
        map.put("orderNo",orderNo);
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        return sysFeeService.getList(p,map);
    }


    //对账管理列表
    @ResponseBody
    @RequestMapping("/excelList")
    public Object excelList(
            Integer status,
            String source,
            String orderNo,
            String beginDateTime,
            String endDateTime,
            HttpServletRequest request) {
        String[] cArray = {"ID", "订单号", "类型", "名称", "操作时间", "订单金额", "手续费支出", "状态", "通道"};
        String[] eArray = {"Id", "OrderNo", "Type", "Username", "Addtime", "DealAmount", "FeeAmount", "Status", "Source"};
        ModelConvert[] converts = {null, null, new SysFeeTypeConvert(),null,
                new ItemAddtimeConvert(), null, null,new AccountCashStatusConvert(), null};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",status);
        map.put("source",source);
        map.put("orderNo",orderNo);
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);

        List<SysFeeVo> contentList = sysFeeService.getList(map);
        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("服务费管理列表", cList, eList, convertList, contentList, serverDir);

        HashMap ret =new HashMap();
        ret.put("result","success");
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }


    @ResponseBody
    @RequestMapping(value ="/statCount",method= RequestMethod.POST)
    public Object statCount(Integer status,
                       String source,
                       String orderNo,
                       String beginDateTime,
                       String endDateTime){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status",status);
        map.put("source",source);
        map.put("orderNo",orderNo);
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        return sysFeeService.statCount(map);
    }
}
